//
//  XiaoXiTongZhiViewController.m
//  JuBoCRM
//
//  Created by iMac on 2024/2/1.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "XiaoXiTongZhiViewController.h"
#import "XiTongTongZhiTableViewCell.h"
#import "ShenPiXiaoXiViewController.h"
#import "NSDate+Helper.h"
@interface XiaoXiTongZhiViewController ()<UITableViewDelegate,UITableViewDataSource>{
    NSDictionary *dataInfoDictionary;
    NSDictionary *kehuDic;
    NSDictionary *shenPiDic;
}
@property (weak, nonatomic) IBOutlet TPKeyboardAvoidingTableView *mainTableView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;

@end

@implementation XiaoXiTongZhiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.customNavBar.title = @"消息通知";
    if (@available(iOS 11.0, *)) {
        self.mainTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    
    self.topSpace.constant = NavaBarHeight;
    
    [_mainTableView registerNib:[UINib nibWithNibName:@"XiTongTongZhiTableViewCell" bundle:nil] forCellReuseIdentifier:@"XiTongTongZhiTableViewCell"];
    
    UIButton *rightButton = [[UIButton alloc]init];
    [rightButton setTitle:@"全部已读" forState:UIControlStateNormal];
    [rightButton setTitleColor:rgba(90, 98, 112, 1) forState:UIControlStateNormal];
    rightButton.titleLabel.font = [UIFont systemFontOfSize:12];
    [self.customNavBar addSubview:rightButton];
    [rightButton addTarget:self action:@selector(yiDuClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    rightButton.sd_layout
    .rightSpaceToView(self.customNavBar, 16)
    .widthIs(50)
    .centerYEqualToView(self.customNavBar.titleLable)
    .heightIs(30);
    
    
    //消息数量显示
    [self huoQuXiaoXiWeiDuShuLiangAndShow:YES];
    
    [self huoQuShenPiLieBiao];
    
    [self huoQuKeHuLieBiao];
}
//全部已读
- (void)yiDuClick{
    [self shenPiYiDu];
}
//审批 3
- (void)shenPiYiDu{
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    [paraDic setValue:@"3" forKey:@"label"];
    [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/adminMessage/readAllMessage" andParas:paraDic andShowHud:NO andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            [self keHuYiDu];
        }
    }];
}
//客户 6
- (void)keHuYiDu{
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    [paraDic setValue:@"6" forKey:@"label"];
    [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/adminMessage/readAllMessage" andParas:paraDic andShowHud:NO andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            [self huoQuXiaoXiWeiDuShuLiangAndShow:NO];
            
            if(_isReadClickBlock){
                _isReadClickBlock();
            }
            
        }
    }];
}

//消息数量显示
- (void)huoQuXiaoXiWeiDuShuLiangAndShow:(BOOL)show{
    [[ZZPRequest getWebUtil] postNetworkRequestURLString:@"/adminMessage/queryUnreadCount" andParas:nil andShowHud:show andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            dataInfoDictionary = [info objectForKey:@"data"];
            [_mainTableView reloadData];
        }
    }];
}

//审批 3
- (void)huoQuShenPiLieBiao{
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    [paraDic setValue:@"1" forKey:@"page"];
    [paraDic setValue:@"10" forKey:@"limit"];
    [paraDic setValue:@"3" forKey:@"label"];
    [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/adminMessage/queryList" andParas:paraDic andShowHud:NO andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            NSDictionary *data = [info objectForKey:@"data"];
            if([[data objectForKey:@"list"] isKindOfClass:[NSArray class]]){
                if([[data objectForKey:@"list"] count] > 0){
                    shenPiDic = [[data objectForKey:@"list"] firstObject];
                }
            }
            
            [_mainTableView reloadData];
        }
    }];
}
//客户 6
- (void)huoQuKeHuLieBiao{
    NSMutableDictionary *paraDic = [NSMutableDictionary dictionary];
    [paraDic setValue:@"1" forKey:@"page"];
    [paraDic setValue:@"10" forKey:@"limit"];
    [paraDic setValue:@"6" forKey:@"label"];
    [[ZZPRequest getWebUtil] postJsonNetworkRequestURLString:@"/adminMessage/queryList" andParas:paraDic andShowHud:NO andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            NSDictionary *data = [info objectForKey:@"data"];
            if([[data objectForKey:@"list"] isKindOfClass:[NSArray class]]){
                if([[data objectForKey:@"list"] count] > 0){
                    kehuDic = [[data objectForKey:@"list"] firstObject];
                }
            }
            [_mainTableView reloadData];
        }
    }];
}




- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    XiTongTongZhiTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XiTongTongZhiTableViewCell"];
    if(indexPath.row == 0){
        cell.itemImage.image = LocalImagePath(@"审批消息");
        cell.titleLabel.text = @"审批消息";
        cell.numLabel.text = GetHandleString(dataInfoDictionary, @"examineCount");
        if([GetHandleString(dataInfoDictionary, @"examineCount") integerValue] > 0){
            cell.redView.hidden = NO;
            cell.contentLabel.hidden = NO;
            cell.timeLabel.hidden = NO;
            cell.contentLabel.text = @"您有待处理的审批，点击处理";
            if([GetHandleString(shenPiDic, @"createTime") length] > 0){
                cell.timeLabel.text = [self huoQuShiJian:GetHandleString(shenPiDic, @"createTime")];
            }else{
                cell.timeLabel.text = @"";
            }
            cell.topSpace.constant = 19;
        }else{
            cell.redView.hidden = YES;
            cell.contentLabel.hidden = YES;
            cell.timeLabel.hidden = YES;
//            cell.contentLabel.text = @"暂无最新消息";
            cell.topSpace.constant = 32;
        }
    }else{
        cell.itemImage.image = LocalImagePath(@"客户消息");
        cell.titleLabel.text = @"客户消息";
        cell.numLabel.text = GetHandleString(dataInfoDictionary, @"crmCount");
        if([GetHandleString(dataInfoDictionary, @"crmCount") integerValue] > 0){
            cell.redView.hidden = NO;
            cell.contentLabel.hidden = NO;
            cell.timeLabel.hidden = NO;
            if([GetHandleString(dataInfoDictionary, @"isQY") integerValue] == 1){
                cell.contentLabel.text = @"您有新的全域客户，点击查看";
                if([GetHandleString(dataInfoDictionary, @"isWGJ") integerValue] == 1){
                    cell.contentLabel.text = @"新入库的全域客户超90分钟未跟进，点击查看";
                }
//                if([GetHandleString(dataInfoDictionary, @"isXSWGJ") integerValue] == 1){
//                    cell.contentLabel.text = @"下属新入库的全域客户超90分钟未跟进，点击查看";
//                }
                if([GetHandleString(dataInfoDictionary, @"isXSWGJ") length] > 0){
                    cell.contentLabel.text = GetHandleString(dataInfoDictionary, @"isXSWGJ");
                }
            }else{
                if([GetHandleString(dataInfoDictionary, @"isXSWGJ") length] > 0){
                    cell.contentLabel.text = GetHandleString(dataInfoDictionary, @"isXSWGJ");
                }else{
                    cell.contentLabel.text = @"您有新的客户，点击查看";
                }
            }
            if([GetHandleString(kehuDic, @"createTime") length] > 0){
                cell.timeLabel.text = [self huoQuShiJian:GetHandleString(kehuDic, @"createTime")];
            }else{
                cell.timeLabel.text = @"";
            }
            cell.topSpace.constant = 19;
        }else{
            cell.redView.hidden = YES;
            cell.contentLabel.hidden = YES;
//            cell.contentLabel.text = @"暂无最新消息";
            cell.timeLabel.hidden = YES;
            cell.topSpace.constant = 32;
        }
    }
    return cell;
}

- (NSString *)huoQuShiJian:(NSString *)time{
    NSString *result = time;
    double timeRes = [[NSDate date] hoursLaterThan:[NSDate dateFromString:time withFormat:@"yyyy-MM-dd HH:mm:ss"]];
    if(timeRes > 0 && timeRes < 1){
        if([[NSDate date] minutesLaterThan:[NSDate dateFromString:time withFormat:@"yyyy-MM-dd HH:mm:ss"]] < 1){
            result = @"1分钟前";
        }else{
            result = [NSString stringWithFormat:@"%.0f分钟前",[[NSDate date] minutesLaterThan:[NSDate dateFromString:time withFormat:@"yyyy-MM-dd HH:mm:ss"]]];
        }
    }else if ([[NSDate dateFromString:time withFormat:@"yyyy-MM-dd HH:mm:ss"] isToday]){
        result = [NSDate dateStringFromString:time format:@"yyyy-MM-dd HH:mm:ss" toFormat:@"HH:mm"];
    }else{
        result = [NSDate dateStringFromString:time format:@"yyyy-MM-dd HH:mm:ss" toFormat:@"yyyy-MM-dd HH:mm"];
    }
    return result;
}





- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ShenPiXiaoXiViewController *shiPi = [[ShenPiXiaoXiViewController alloc] init];
    if(indexPath.row == 0){
        shiPi.isShenPi = YES;
    }else{
        shiPi.isShenPi = NO;
    }
    shiPi.readClickBlock = ^{
        //消息数量显示
        [self huoQuXiaoXiWeiDuShuLiangAndShow:NO];
        
        [self huoQuShenPiLieBiao];
        
        [self huoQuKeHuLieBiao];
        
        if(_isReadClickBlock){
            _isReadClickBlock();
        }
        
    };
    [self pushViewController:shiPi];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end




